using System;
using System.Collections.Generic;
using System.Linq;
namespace WebClient.Controllers
{
	#region using
	using System.Web.Mvc;
	using WebClient.Models.ViewModels;
	using WebClient.Models;
	using WebClient.Models.Repositories;
	using WebClient.Helpers;
	using System.Web.Security;
    using WebClient.CF_DAL_WS;
	#endregion
	public partial class AdminController: BaseController
	{
		public ActionResult SelectUsers()
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_USERS
			};
			return View(CreateViewModel(data));
		}


		[AcceptVerbs(HttpVerbs.Delete)]
		public JsonResult DeleteUsers(Guid id, string role, int idrestaurant = -1)
		{
            if (role.ToUpper() == "MANAGER" || idrestaurant != -1)
            {
                // Delete also in restaurant
                var restaurant = _repRestaurant.SelectByID(idrestaurant);
                restaurant.IDManager = new Guid();
                var result = _repRestaurant.Update(restaurant);
            }
			return Json(new{
				Success = _repUsers.Delete(id),
				RecordCount = _repUsers.GetTotalPage(role),
				DeleteId = id
			});
		}

		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult UpdateUsers(FormCollection forms)
		{
			string username = forms.GetValues("Users_UserName")[0];
			string email = forms.GetValues("Users_Email")[0];
			string id = forms.GetValues("Users_ID")[0];
			string name = forms.GetValues("Users_Name")[0];
			string phone = forms.GetValues("Users_Phone")[0];
			string currentpage = forms.GetValues("Users_CurrentPage")[0];
			var user = Membership.GetUser(username);
			if (email != user.Email)
			{
				user.Email = email;
				Membership.UpdateUser(user);
			}
			var Users = _repUsers.SelectByID(Guid.Parse(id));
			if (Users == null)
			{
                Users = new UsersDTO
				{
					ID = Guid.Parse(id),
					Name = name,
					Phone = phone
				};
                //_repUsers.Insert(Users, );
			}
			else
			{
				Users.Name = name;
				Users.Phone = phone;
			    _repUsers.Update(Users);
			}
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_USERS,
				CurrentPage = int.Parse(currentpage)
			};
			return PartialView("Templates/TH_ListUsers", CreateViewModel(data));
		}

		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult InsertUsers(FormCollection forms)
		{
			var nguoiDungInfo = new AddUsersViewModel
			{
                UserName = forms.GetValues("Users_UserName")[0],
                Password = forms.GetValues("Users_Password")[0],
                Email = forms.GetValues("Users_Email")[0],
                Role = new RoleDTO
                {
                    Name = forms.GetValues("Users_Role")[0]
                }
			};
			var result = _repUsers.Insert(nguoiDungInfo);
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_USERS,
				Role = forms.GetValues("Users_OwnerRole")[0]
			};
			return Json(new{
				Success = (result == string.Empty),
                Message = result == string.Empty ? "A new item has been added!" : result,
				PartialViewHtml = RenderPartialViewToString("Templates/TH_ListUsers", CreateViewModel(data))
			});
		}

		public ActionResult DetailOfUsers(Guid id)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_USERS,
				GuidID =id
			};
			return View(CreateViewModel(data));
		}

        /////////////////////////////////////// MANAGER ///////////////////////////////////////////
        public ActionResult SelectManager(int idrestaurant)
        {
            var data = new DataTransferViewModel
            {
                EnumViewModelType = EnumViewModel.ADMIN_MANAGER,
                IDRestaurant = idrestaurant
            };
            return View(CreateViewModel(data));
        }

        [ValidateInput(false)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult InsertManager(FormCollection forms)
        {
            var nguoiDungInfo = new AddUsersViewModel
            {
                UserName = forms.GetValues("Users_UserName")[0],
                Password = forms.GetValues("Users_Password")[0],
                Email = forms.GetValues("Users_Email")[0],
                Role = new RoleDTO
                {
                    Name = "Manager"
                }
            };
            var idRestaurant = int.Parse(forms.GetValues("Restaurant")[0]);
            var result = _repUsers.Insert(nguoiDungInfo);
            if (result == string.Empty)
            {
                var newManager = _repUsers.SelectByUserName(nguoiDungInfo.UserName);
                var restaurant = _repRestaurant.SelectByID(idRestaurant);
                restaurant.IDManager = newManager.ID;
                _repRestaurant.Update(restaurant);
            }
            var data = new DataTransferViewModel
            {
                EnumViewModelType = EnumViewModel.ADMIN_MANAGER,
                IDRestaurant = idRestaurant,
                InfoText = result == string.Empty ? "New item has been added" : result
            };
            return View("SelectManager", CreateViewModel(data));
        }
	}
}